#
class Rect:
    def __init__(self):
        self.width = 0
        self.height = 0

    def set_size(self, size):
        self.width, self.height = size

    def get_size(self):
        return self.width, self.height


r = Rect()
r.set_size((10, 4))
print(r.width, r.height)  # 10 4


# property

class Rect2:
    def __init__(self):
        self.width = 0
        self.height = 0

    def set_size(self, size):
        self.width, self.height = size

    def get_size(self):
        return self.width, self.height

    size = property(get_size, set_size)


r = Rect2()
r.width = 10
r.height = 5
print(r.size)  # (10, 5)


#

# 静态方法和类方法

#  使用staticmethod包装成静态方法
# 使用cmeth包装成类方法
class MyClass:
    def smeth():
        print("this is a static method")

    smeth = staticmethod(smeth)

    def cmeth(cls):
        print("this is a class of ", cls)

    cmeth = classmethod(cmeth)


MyClass.smeth()  # this is a static method
MyClass.cmeth()  # this is a class of  <class '__main__.MyClass'>


# 使用装饰器语法优化
class MyClass2:
    @staticmethod
    def smeth():
        print("this is a static method")

    @classmethod
    def cmeth(cls):
        print("this is a class of ", cls)


MyClass2.smeth()  # this is a static method
MyClass2.cmeth()  # this is a class of  <class '__main__.MyClass2'>

# __getattr__  在访问属性时被调用
# __setattr__ 在设置属性时被调用
# __getattribute__(self,name) 在访问属性时被调用
# __delattr__ 在删除属性时被调用


